<template>
  <el-select
    ref="select"
    v-model="dicValue"
    :placeholder="dataList.Placeholder"
    @change="change"
  >
    <el-option
      v-for="item in dicList"
      :key="valueIsIdOrCode ? item.id : item.value"
      :label="item.name"
      :value="valueIsIdOrCode ? item.id : item.value"
    >
    </el-option>
  </el-select>
</template>

<script>
import {getDictList} from "@/ModuleSystem/api/System/SystemDict.js";
export default {
  name: "DicDropDownList",
  components: {},
  props: {
    // 查询条件下是否需要全部
    // 筛选条件
    filterType: {
      type: String,
      default: "",
    },
    isAll: {
      type: Boolean,
      default: false,
    },
    // 使用id或者使用code绑定
    valueIsIdOrCode: {
      type: Boolean,
      default: true,
    },
    dictCode: {
      type: String,
      default: "",
    },
    value: {
      type: String,
      default: "",
    },
    displayText: {
      type: Array,
      default: () => [],
    },
    notDisplayText: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      dicList: [],
      dicValue: this.value,
    };
  },
  computed: {
    dataList() {
      return this.$t("ModuleDevice.Device");
    },
  },
  watch: {
    value(val) {
      this.dicValue = val;
    },
  },
  mounted() {
    this.initData();
  },
  methods: {
    change() {
      const selectedOption = this.dicList.find(
        (item) =>
          (this.valueIsIdOrCode ? item.id : item.value) === this.dicValue
      );
      const selectedLabel = selectedOption ? selectedOption.name : "";

      this.$emit("input", this.dicValue, selectedLabel);
    },
    initData() {
      getDictList({ dictCode: this.dictCode })
        .then((res) => {
          this.dicList = res.data;
          if (
            this.notDisplayText !== null &&
            this.notDisplayText.length !== 0
          ) {
            this.dicList = this.dicList.filter((item) => {
              return this.notDisplayText.indexOf(item.name) < 0;
            });
          }

          if (this.displayText !== null && this.displayText.length !== 0) {
            this.dicList = this.dicList.filter((item) => {
              return this.displayText.indexOf(item.name) >= 0;
            });
          }

          if (this.isAll) {
            var obj = {};
            obj.id = "-1";
            obj.value = "-1";
            obj.name = this.$t("ModuleDevice.components.all");
            this.dicList.unshift(obj);
          }
        });
    },
  },
};
</script>
<style scoped></style>
